package com.atxiao.service.JUC;

class Share{
    private int number=0;
    public synchronized void decr(){
        if (number!=1){
            //线程进行等待（wait会释放锁）
            try {
                this.wait();
            } catch (InterruptedException e) {
            }
        }

        this.number--;
        System.out.println(Thread.currentThread().getName()+":"+number);
        this.notifyAll();

    }

    public synchronized void incr(){
        if (number!=0){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.number++;
        System.out.println(Thread.currentThread().getName()+":"+number);
        this.notifyAll();
    }


}


public class ThreadNotice {
    public static void main(String[] args) {
        Share share = new Share();
        new Thread(()->{
            for (int i = 0; i < 20; i++) {
                share.incr();
            }
        },"AA").start();
        new Thread(()->{
            for (int i = 0; i < 20; i++) {
                share.decr();
            }

        },"BB").start();

        new Thread(()->{
            for (int i = 0; i < 20; i++) {
                share.incr();
            }
        },"cc").start();
        new Thread(()->{
            for (int i = 0; i < 20; i++) {
                share.decr();
            }

        },"DD").start();
    }



}
